package cn.edu.ysu.zry.handle;

import cn.edu.ysu.zry.config.ConfigZhongxin;
import cn.edu.ysu.zry.entity.ZhongJiedian;
import cn.edu.ysu.zry.util.SHA256;

import java.util.Base64;
import java.util.Scanner;

/**
 * @program: XieyiBlockChainOS
 * @description: 重节点启动后的操作类
 * @author: Ruiyuan Zhang
 * @create: 2020-04-21 13:48
 */
public class HandleZhongxin {

    private static class HANDLE{
        public static final String SHOW_PUBLIC_KEY="showPublicKey";
        public static final String SHOW_PRIVATE_KEY="showPrivateKey";
        public static final String EXIT="exit";
        public static final String SHOW_APPLY_LIST="showApplyList";
        public static final String SHOW_ZHONGJIEDIAN_LIST="showZhongJiedianList";
        public static final String ADD_ZHONGJIEDIAN_LIST="addZhongJiedian";
    }


    /**
    *@Description: 启动对于中心节点的操作类 - 主控制
    *@Param:
    *@return:
    *@Author: Ruiyuan Zhang
    *@date: 2020/4/21
    */
    public static void startup(){
        Scanner input=new Scanner(System.in);
        System.out.println("                                       ");
        System.out.println("---------------------------------------");
        System.out.println("     [XieyiBlockChainOS - Zhongxi]     ");
        System.out.println("                                       ");
        System.out.println("              Ruiyuan Zhang            ");
        System.out.println("                  v0.1                 ");
        System.out.println("               2020-04-22              ");
        System.out.println("                                       ");
        System.out.println("---------------------------------------");
        System.out.println("                                       ");
        while (true){
            String str=input.next();
            String handleStrs[]=str.split(" ");
            for (String x:handleStrs) {
                x=x.trim();
            }
            String handleStr=handleStrs[0];
            if(handleStr.equals(HANDLE.SHOW_PUBLIC_KEY)){
                System.out.println("公钥："+ Base64.getEncoder().encodeToString(ConfigZhongxin.PUBLIC_KEY));
            }else if(handleStr.equals(HANDLE.SHOW_PRIVATE_KEY)){
                System.out.println("私钥："+ Base64.getEncoder().encodeToString(ConfigZhongxin.PRIVATE_KEY));
            }else if(handleStr.equals(HANDLE.SHOW_APPLY_LIST)){
                for (ZhongJiedian zhongJiedian:ConfigZhongxin.applyZhongJiedianArrayList ){
                    System.out.println("公钥："+Base64.getEncoder().encodeToString(zhongJiedian.getPublicKey()));
                }
            }else if(handleStr.equals(HANDLE.ADD_ZHONGJIEDIAN_LIST)){
                String publicKey=handleStrs[1];
                int index=-1;
                for(int i=0;i<ConfigZhongxin.applyZhongJiedianArrayList.size();i++){
                    if(Base64.getEncoder().encodeToString(ConfigZhongxin.applyZhongJiedianArrayList.get(i).getPublicKey()).equals(publicKey)){
                        index=i;
                    }
                }
                if(index==-1){
                    System.out.println("没有您要添加的数据");
                    continue;
                }
                ConfigZhongxin.zhongJiedianArrayList.add(ConfigZhongxin.applyZhongJiedianArrayList.get(index));
                ConfigZhongxin.applyZhongJiedianArrayList.remove(index);
            }else if(handleStr.equals(HANDLE.SHOW_ZHONGJIEDIAN_LIST)){
                for (ZhongJiedian zhongJiedian:ConfigZhongxin.zhongJiedianArrayList ){
                    System.out.println("公钥："+Base64.getEncoder().encodeToString(zhongJiedian.getPublicKey()));
                }
            }else if(handleStr.equals(HANDLE.EXIT)){
                break;
            }
        }
        input.close();
    }

}
